﻿using ETA.ClientService;
using ETA.Common;
using ETA.DomainEnum;
using ETA.Entity;
using ETA.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ETA.Web.Controllers
{
    public class HomeController : BaseController
    {
        //
        // GET: /Home/

        private static IWebMessageService _IWebMessageService { set; get; }
        private static IMBTIService _IMBTIService { set; get; }

        public HomeController(IWebMessageService IWebMessageService, IMBTIService IMBTIService)
        {
            _IWebMessageService = IWebMessageService;
            _IMBTIService = IMBTIService;
        }


        public ActionResult Index()
        {
            return View();
        }


        public ActionResult GetNotReadMessage()
        {
            var count = 0;
            if (CurrentUser != null)
            {
                var queryRes = _IWebMessageService.QueryWebMessageList(new QueryWebMessageRequest
                {
                    WebMessage = new Entity.WebMessage
                    {
                        UserId = CurrentUser.UserId
                    }
                });
                if (queryRes.Result.IsSuccess)
                {
                    count = queryRes.WebMessageList.Count(w => w.MessageOpenEnum == DomainEnum.MessageOpenEnum.未读);
                }
            }
            return JsonWithDate(count);
        }

        /// <summary>
        /// 头部分部视图
        /// </summary>
        /// <returns></returns>
        public ActionResult HeaderPart()
        {
            return PartialView(base.CurrentUser);
        }

        /// <summary>
        /// 出国特色
        /// </summary>
        /// <returns></returns>
        public ActionResult SpecialOfETA()
        {
            return View();
        }

        /// <summary>
        /// 联系我们
        /// </summary>
        /// <returns></returns>
        public ActionResult ContactUs()
        {
            return View();
        }

        /// <summary>
        /// 关于出国
        /// </summary>
        /// <returns></returns>
        public ActionResult AboutETA()
        {
            return View();
        }

        /// <summary>
        /// 项目介绍
        /// </summary>
        /// <returns></returns>
        public ActionResult IntroduceOfProject()
        {
            return View();
        }


        /// <summary>
        /// 出国流程
        /// </summary>
        /// <returns></returns>
        public ActionResult FlowOfGoJpan()
        {
            return View();
        }


        /// <summary>
        /// 才储数据接收
        /// </summary>
        /// <param name="reportid">报告id</param>
        /// <param name="liagnbiao">量表</param>
        /// <param name="test_email">考试者email</param>
        /// <returns></returns>
        public ActionResult AsyncMBITResult(string reportid, string liangbiao, string test_email)
        {
            var res = new Response();
            if (!string.IsNullOrEmpty(reportid))
            {
                var qRes = _IMBTIService.QueryMBTIList(new QueryMBTIRequest
                {
                    ReportId = reportid
                });

                if (qRes.Result.IsSuccess)
                {
                    if (!qRes.MBTIs.Any())//不存在时则添加
                    {
                        var createRes = _IMBTIService.CreateMBTI(new CreateMBTIRequest
                        {
                            MBTI = new MBTI
                            {
                                CreateDate = DateTime.Now,
                                Email = test_email,
                                MBTIModel = liangbiao,
                                ReportId = reportid,
                                UserId = CurrentUser.UserId
                            }
                        });

                        res.Result = createRes.Result;
                    }
                    else
                    {
                        res.Result.ErrMessage = string.Format("报告信息{0}已存在，无需重复添加", reportid);
                    }
                }
            }
            if (res.Result.IsSuccess)
            {
                var subLog = new Response();
                if (liangbiao.Trim().ToUpper() == Config.OnlineTestKind)
                {
                    subLog = RegistrationMainFormBll.SubmitFlowLog(CurrentUser.UserId, 2, FlowLogStateEnum.进行中);
                }

                if (subLog.Result.IsSuccess)
                {
                    return View("AsyncMBITResultSuccess");
                }
                else
                {
                    ViewBag.Errmessage = subLog.Result.ErrMessage;
                    return View("AsyncMBITResultFail");
                }

            }
            else
            {
                ViewBag.Errmessage = res.Result.ErrMessage;
                return View("~/Views/Home/AsyncMBITResultFail.cshtml");
            }
        }

    }
}
